import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/29 10:10
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] forbidden = {1,6,2,14,5,17,4};
        solution.minimumJumps(forbidden, 16, 9, 7);
    }

    public int minimumJumps(int[] forbidden, int a, int b, int x) {

        final int LIMIT = 8000;
        //当前位置，当前向后跳次数
        boolean[][] visited = new boolean[8001][2];
        Set<Integer> forbid = new HashSet<>(forbidden.length);
        for (int i : forbidden) {
            forbid.add(i);
        }
        // [ 当前位置, 向后跳次数 ]
        Deque<int[]> q = new LinkedList<>();
        q.offer(new int[]{0, 0});
        int layer = -1;
        while (!q.isEmpty()) {
            int qs = q.size();
            layer++;
            for (int i = 0; i < qs; i++) {
                int[] p = q.poll();
                int cur = p[0], backwardCount = p[1];
                if (cur == x) {
                    return layer;
                }
                if (visited[cur][backwardCount]) {
                    continue;
                }
                visited[cur][backwardCount] = true;

                if (cur + a <= LIMIT && !forbid.contains(cur + a)) {
                    q.offer(new int[]{cur + a, 0});
                }
                if (cur - b >= 0 && backwardCount < 1 && !forbid.contains(cur - b)) {
                    q.offer(new int[]{cur - b, backwardCount + 1});
                }
            }
        }
        return -1;
    }
}
